﻿

class WebLife {  
    static timerToken1: number;
    static NrOfDrawAreas = 10;      // 10
    static NrOfDrawContents = 3;    // 2
    static DrawAreas: Array<IDrawArea>;
    static GenerateContents: Array<IGenerateContent>;
    static MapAreasToContents: Array<number>;
    static BackgroundColors = ["#0000ff", "#88bbdd", "#66bb44", "#ffee00", "#ff4411", "#88bbdd", "#66bb44", "#ffee00", "#ff4411", "#853933"];

    static Init() {
        WebLife.DrawAreas = new Array<IDrawArea>();
        WebLife.GenerateContents = new Array<IGenerateContent>();
        WebLife.MapAreasToContents = new Array<number>();

        for (var i = 0; i < WebLife.NrOfDrawAreas; i++) {
            WebLife.DrawAreas.push(new DrawArea("S" + i.toString(), WebLife.BackgroundColors[i]));
            WebLife.DrawAreas[i].CanvasToHtml();
            WebLife.MapAreasToContents.push(-1);
        }

        WebLife.GenerateContents.push(new GameOfLife());
        WebLife.GenerateContents.push(new WolfsAndSheeps());
        WebLife.GenerateContents.push(new Swarm());
        WebLife.start();

    }

    static Counter: number = 0;
    static SmallCounter: number = 0;
    static LastSmallCounter: number = -1;

    static Run() {
        WebLife.Counter += 1;
        WebLife.SmallCounter = Math.floor(WebLife.Counter / 100);

        if (WebLife.SmallCounter != WebLife.LastSmallCounter) {
            WebLife.CreateNewMapAreasToContents();
            WebLife.SetUnusedAreasToHtml();
        }

        WebLife.LastSmallCounter = WebLife.SmallCounter;

        WebLife.DrawUsedAreas();
   
    }

    static CreateNewMapAreasToContents() {
        for (var i = 0; i < WebLife.NrOfDrawAreas; i++) {
            WebLife.MapAreasToContents[i] = -1;
        }


        for (var i = 0; i < WebLife.NrOfDrawContents; i++) {
            var drawingArea = Math.floor(Math.random() * (WebLife.NrOfDrawAreas));
            WebLife.MapAreasToContents[drawingArea] = i;         
        }
    }

    static SetUnusedAreasToHtml() {
        for (var i = 0; i < WebLife.NrOfDrawAreas; i++) {
            if (WebLife.MapAreasToContents[i] === -1)
                WebLife.DrawAreas[i].CanvasToHtml();
            else
                WebLife.DrawAreas[i].HtmlToCanvas();
        }
    }

    static DrawUsedAreas() {
        for (var i = 0; i < WebLife.NrOfDrawAreas; i++) {
            if (WebLife.MapAreasToContents[i] !== -1)
                WebLife.GenerateContents[WebLife.MapAreasToContents[i]].GenerateContent(WebLife.DrawAreas[i]);
        }
    }

    static start() {
       
        WebLife.timerToken1 = setInterval(() => WebLife.Run(), 100);
    }

    static stop() {
        clearTimeout(WebLife.timerToken1);
    }

}





window.onload = () => {
    WebLife.Init();
    //GameOfLife.Init();
    //GameOfLife.Run(true);
};

window.onresize = () => {

    //GameOfLife.Run(false);
};